package com.itheima.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.sql.SQLClientInfoException;
import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author chm
 * @version 1.0.0 v
 * @date 2022-08-14 9:37
 */

@ControllerAdvice(annotations = {RestController.class,Controller.class})
@ResponseBody
@Slf4j

public class GlobalExceptionHandler {

    @ExceptionHandler(SQLClientInfoException.class)

    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
        log.info(ex.getMessage());

        if (ex.getMessage().contains("Duplicate entry")){
            String[] split = ex.getMessage().split("");
            String msg =split[2]+"存在";
            R.error(msg);

        }
        return R.error("对错误不清楚");
    }


    @ExceptionHandler(CustomException.class)

    public R<String> exceptionHandler(CustomException ex){
        return R.error(ex.getMessage());
    }

}
